Repository navigation
[release/10.0.1xx] [runtime] Avoid weak-reference deadlocks during fork. Fixes #26844 - #27051
Conversation
…26863) Resolving an Objective-C weak reference takes a side-table lock before calling our `retainWeakReference` override. For dynamically registered types, reading the managed wrapper's finalization flags then calls `objc_getAssociatedObject`, which takes the association lock. Objective-C's fork preparation acquires those locks in the opposite order, so concurrent UI layout and `Process.Start()` can deadlock. Cache `NSObjectData` alongside GC handles and flags in the existing native dictionary, so the weak-reference callback no longer needs the associated-object API. Preserve the cached data when a GC handle is temporarily cleared, including during `copyWithZone`, and remove the entry during native deallocation before its address can be reused. Debug logging on this path also avoids Objective-C class queries. Deallocation cleanup checks the startup-configured dynamic-registration support flag before taking the dictionary mutex, avoiding unnecessary locking when dynamic registration is disabled without an unsynchronized dictionary-pointer read. NativeAOT excludes this cleanup at compile time. Fixes #26844 🤖 Pull request created by Copilot --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: Rolf Bjarne Kvinge <rokvin@microsoft.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🔵 Needs a closer look
Native lock-order and cached NSObjectData lifetime changes require macOS dynamic-registrar stress validation of concurrent weak-reference resolution, fork safety, and deallocation, which is unavailable in this review environment.
No open issues 🎉
What changed in this PR
Backports the runtime fix for #26844 to release/10.0.1xx, addressing deadlocks when Objective-C weak-reference resolution overlaps with process creation.
Changes:
- Cache
NSObjectDataalongside GC handles to avoid associated-object lookups during weak-reference callbacks. - Preserve cached data when GC handles are cleared, remove it during native deallocation, and avoid Objective-C class queries in affected debug logging.
- Add regression tests for lock-independent lookup and cached-data lifecycle.
| File | Description |
|---|---|
tests/test-libraries/libtest.m |
Adds a native helper to test data lookup while the association lock is held. |
tests/test-libraries/libtest.h |
Declares the native regression-test helper. |
tests/monotouch-test/ObjCRuntime/NativeRuntimeTest.cs |
Tests lock-independent lookup, GC-handle updates, and deallocation cleanup. |
runtime/trampolines.m |
Implements cached data lookup, lifecycle cleanup, and safer debug logging. |
Review effort: Balanced
✅ API diff for current PR / commitNET (empty diffs)✅ API diff vs stableNET (empty diffs)ℹ️ Generator diffGenerator Diff: vsdrops (html) vsdrops (raw diff) gist (raw diff) - Please review changes) Pipeline on Agent |
This comment has been minimized.
This comment has been minimized.
🚀 [CI Build #057e144] Test results 🚀Test results✅ All tests passed on VSTS: test results. 🎉 All 200 tests passed 🎉 Tests counts✅ assembly-processing: All 1 tests passed. Html Report (VSDrops) Download macOS tests✅ Tests on macOS Sonoma (14): All 5 tests passed. Html Report (VSDrops) Download Linux Build VerificationPipeline on Agent |
Summary
Resolving an Objective-C weak reference takes a side-table lock before calling our
retainWeakReferenceoverride. For dynamically registered types, reading the managed wrapper's finalization flags then callsobjc_getAssociatedObject, which takes the association lock. Objective-C's fork preparation acquires those locks in the opposite order, so concurrent UI layout andProcess.Start()can deadlock.Cache
NSObjectDataalongside GC handles and flags in the existing native dictionary, so the weak-reference callback no longer needs the associated-object API. Preserve the cached data when a GC handle is temporarily cleared, including duringcopyWithZone, and remove the entry during native deallocation before its address can be reused. Debug logging on this path also avoids Objective-C class queries.Deallocation cleanup checks the startup-configured dynamic-registration support flag before taking the dictionary mutex, avoiding unnecessary locking when dynamic registration is disabled without an unsynchronized dictionary-pointer read. NativeAOT excludes this cleanup at compile time.
Fixes #26844
🤖 Pull request created by Copilot
Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com
Co-authored-by: Rolf Bjarne Kvinge rokvin@microsoft.com
Co-authored-by: Copilot Autofix powered by AI 175728472+Copilot@users.noreply.github.com
Backport of #26863.